package org.example;

import java.util.HashMap;
import java.util.Map;

public class Test1 {
    //leetcode 只出现一次的数字II
    //https://leetcode.cn/problems/single-number-ii/description/?envType=problem-list-v2&envId=array

    /**
     * 哈希表记录元素出现的次数
     * @param nums
     * @return
     */
    public int singleNumber(int[] nums) {
        Map<Integer,Integer> hash = new HashMap<>();
        for (int n : nums) {
            hash.put(n,hash.getOrDefault(n,0) + 1);
        }
        for (Map.Entry<Integer,Integer> entry : hash.entrySet()) {
            int key = entry.getKey(), value = entry.getValue();
            if (value == 1) return key;
        }
        return -1;
    }
}
